<?php
/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/8/22
 * Time: 20:37
 */
namespace App\Services\LandingRepaymentE;

class LandingCollectMoneyEService
{
    protected $landingECollectMoneyRegisterApi;
    protected $landingECollectMoneyQuickPayApi;
    protected $landingECollectMoneyQuickModifyFeeApi;
    protected $landingCSecret;
    protected $landingCMerchno;
    protected $landingECollectMoneyQuickReturnUrl;
    protected $landingCWCollectMoneyQuickNotifyUrl;

    private $orderNo;//订单号
    private $returnUrl;//跳转返回url
    private $notifyUrl;//回调url
    private $realname;//真实姓名
    private $idCardNo;//身份证号
    private $mobile;//手机号码
    private $bankCardNo;//银行卡号
    private $settleBankCard;//银行卡号
    private $bankCardCode;//银行卡号
    private $subMerchantNo;//子商户号
    private $amount;//金额
    private $fixAmount;//金额
    private $userFee;//用户费率 金额
    private $bankName;//银行名称
    private $cvn2;//cvn2
    private $expireDate;//卡过期时间
    private $transtype;

    public function __construct()
    {
        $this->landingECollectMoneyRegisterApi = config('landingrepaymente.landing_e_collect_money_register_api');//大额e接口
        $this->landingECollectMoneyQuickPayApi = config('landingrepaymente.landing_e_collect_money_quick_pay_api');//落地e快捷支付接口
        $this->landingECollectMoneyQuickModifyFeeApi = config('landingrepaymente.landing_e_collect_money_quick_modify_fee_api');//落地e修改费率接口
        $this->landingCSecret = config('landingrepaymente.landing_e_secret');//商户密钥
        $this->landingCMerchno = config('landingrepaymente.landing_e_merchno');//商户id
        $this->landingECollectMoneyQuickReturnUrl = config('landingrepaymente.landing_e_collect_money_return_url');//返回跳转url
        $this->landingECollectMoneyQuickNotifyUrl = config('landingrepaymente.landing_e_collect_money_notify_url');//收款回调
    }

    /**
     * 请求参数
     * @return array
     */
    protected function requestParams($transcode = '404')
    {
        $params =  [
            'transcode' => $transcode,
            'version' => '001',
            'merchno' => $this->landingCMerchno,
            'dsorderid' => $this->orderNo,
            'returnUrl' => $this->returnUrl,
            'notifyUrl' => $this->notifyUrl,
            'futureRateValue' => $this->userFee,
            'fixAmount' => $this->fixAmount,
            'accountName' => $this->realname,
            'idcard' => $this->idCardNo,//身份证号
            'mobile' => $this->mobile,
            'settleBankCard' => $this->settleBankCard,
            'settleBankName' => $this->bankName,
            'settleBankCode' => $this->bankCardCode,
            'amount' => $this->amount,
            'bankcard' => $this->bankCardNo,
            'subMerchantNo' => $this->subMerchantNo,//子商户
            'cvn2' => $this->cvn2,
            'expireDate' => $this->expireDate,
            'transtype' => $this->transtype,
        ];
        if (null == $transcode)
            unset($params['transcode']);
        if (null == $this->orderNo)
            unset($params['dsorderid']);
        if (null == $this->returnUrl)
            unset($params['returnUrl']);
        if (null == $this->notifyUrl)
            unset($params['notifyUrl']);
        if (null == $this->realname)
            unset($params['accountName']);
        if (null == $this->idCardNo)
            unset($params['idcard']);
        if (null == $this->mobile)
            unset($params['mobile']);
        if (null == $this->bankCardNo)
            unset($params['bankcard']);
        if (null == $this->settleBankCard)
            unset($params['settleBankCard']);
        if (null == $this->bankName)
            unset($params['settleBankName']);
        if (null == $this->bankCardCode)
            unset($params['settleBankCode']);
        if (null == $this->subMerchantNo)
            unset($params['subMerchantNo']);
        if (null == $this->amount)
            unset($params['amount']);
        if (null == $this->fixAmount)
            unset($params['fixAmount']);
        if (null == $this->userFee)
            unset($params['futureRateValue']);
        if (null == $this->cvn2)
            unset($params['cvn2']);
        if (null == $this->expireDate)
            unset($params['expireDate']);
        if (null == $this->transtype)
            unset($params['transtype']);
        return $params;
    }

    /**
     * 获取接口请求结果
     * @param $param
     * @param $url
     * @return mixed
     */
    protected function getRequestResult($param, $url)
    {
        // 根据key排序数组
        ksort($param);
        // 转换成字符串 格式为：key=valkey2=val2
        $singleStr = $this->toUrlParams($param);
        // 加密字符拼接秘钥进行md5加密，得到签名
        $param['sign'] = md5($singleStr . $this->landingCSecret);
        $client = new \GuzzleHttp\Client(['verify' => false]);
        $response = $client->request('post', $url, ['json' => $param]);
        $json = json_decode($response->getBody()->getContents(), true);
        return $json;
    }

    /**
     * 格式化参数
     * @param $param
     * @return string
     */
    protected function toUrlParams($param){
        $string = '';
        foreach ($param as $k=>$v){
            if(is_array($v)){
                $s = '';
                foreach ($v as $key=>$val){
                    $s .= $key.'=' . $val . ', ';
                }
                $s = substr($s,0,-2);
                $string .= $k . '=' . '{'. $s .'}';
            }else{
                $string .= $k . '=' . $v;
            }
        }
        return $string;
    }

    /**
     * 子商户注册
     * "transcode" => "025"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912378621272977"
     * "sign" => "4c939ffe949282796b082a13daebcea2"
     * "returncode" => "0000"
     * "errtext" => "提交成功"
     * "merchantName" => "刘辉武"
     * "subMerchantNo" => "jOz9tPAPBP"
     *
     * "transcode" => "025"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912385810513184"
     * "sign" => "f775e131287f67b732829131d9b70a81"
     * "returncode" => "03008"
     * "errtext" => "法人身份信息重复进件"
     *
     * "transcode" => "025"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912372647596738"
     * "sign" => "cf48dd8e598af35011cced16ef79eabe"
     * "returncode" => "0097"
     * "errtext" => "交易出错 ：java.lang.NullPointerException"
     * "transtype" => "30"
     * @param $orderNo
     * @param $trueName
     * @param $mobile
     * @param $idCard
     * @param $bankCardNo
     * @param $bankCardName
     * @param $consumeAddress
     * @return mixed
     */
    public function subMerchantRegister($orderNo, $realname, $mobile, $idCard,
                                        $bankCardNo, $bankCardName, $bankCardCode,
                                        $userFee, $amount)
    {
        $this->orderNo = $orderNo;
        $this->userFee = $userFee;
        $this->fixAmount = $amount;
        $this->realname = $realname;
        $this->idCardNo = $idCard;
        $this->mobile = $mobile;
        $this->settleBankCard = $bankCardNo;
        $this->bankName = $bankCardName;
        $this->bankCardCode = $bankCardCode;
        $dataContent = $this->requestParams('040');
        return $this->getRequestResult($dataContent, $this->landingECollectMoneyRegisterApi);
    }

    /**
     * 快捷支付
     * array:11 [
     * "transcode" => "026"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912920633712329"
     * "sign" => "1d5841652255afe1fe93675e82aec1b6"
     * "returncode" => "0000"
     * "errtext" => "下单成功"
     * "orderid" => "2018091202504100100100241"
     * "amount" => "1000"
     * "transtype" => "18"
     * "pay_info" => "http://pay.huanqiuhuiju.com/authsys/api/tl/tradePage.do"
     * "pay_code" => "{"orderid":"2018091202504100100100241","merchno":"lmkj25se267f69f","bankcard":"62257686 **** 7527","accountName":"胡*杰","mobile":"139 **** 1684","returnUrl":"https://passport.longmaoguanjia.com/api/notify/landingc/quick_money_return","notifyUrl":"https://passport.longmaoguanjia.com/api/notify/landingc/quick_money_notify","sign":"06e487717a4e03304bfd3a9f76b62b8a"}"
     * ]
     * @param $orderNo
     * @param $realname
     * @param $mobile
     * @param $bankCardNo
     * @param $amount
     * @param $cvn2
     * @param $expireDate
     * @param $subMerchantNo
     * @return mixed
     */
    public function quickPay($orderNo, $realname, $mobile,
                             $bankCardNo, $amount, $cvn2, $expireDate, $subMerchantNo)
    {
        $this->orderNo = $orderNo;
        $this->amount = $amount;
        $this->realname = $realname;
        $this->mobile = $mobile;
        $this->bankCardNo = $bankCardNo;
        $this->cvn2 = $cvn2;
        $this->expireDate = $expireDate;
        $this->subMerchantNo = $subMerchantNo;
        $this->returnUrl = $this->landingECollectMoneyQuickReturnUrl . '?orderNo=' . $orderNo;
        $this->notifyUrl = $this->landingECollectMoneyQuickNotifyUrl;
        $dataContent = $this->requestParams('041');
        return $this->getRequestResult($dataContent, $this->landingECollectMoneyQuickPayApi);
    }

    /**
     * 快捷修改收款卡号和费率
     * "transcode" => "027"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912354133100334"
     * "sign" => "5bb3089f1f1f9bdd3954243988389bc8"
     * "returncode" => "0001"
     * "errtext" => "商户手续费必须在0.45到1之间"
     *
     * "transcode" => "027"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912356809440502"
     * "sign" => "30cfabbccb06d686aec83401597c8383"
     * "returncode" => "0001"
     * "errtext" => "交易失败:结算卡类型错误"
     *
     * "transcode" => "027"
     * "merchno" => "lmkj25se267f69f"
     * "dsorderid" => "A912363221434387"
     * "sign" => "6823f857eb7999668d5a9e8084058134"
     * "returncode" => "0000"
     * "errtext" => "成功:处理成功"
     * @param $orderNo
     * @param $amount
     * @param $rate
     * @param $realname
     * @param $mobile
     * @param $bankCardNo
     * @param $bankCardCode
     * @param $bankCardName
     * @param $subMerchantNo
     * @return mixed
     */
    public function quickModifyFee($orderNo, $amount, $rate, $realname, $mobile,
                             $bankCardNo, $bankCardCode, $bankCardName, $subMerchantNo)
    {
        $this->orderNo = $orderNo;
        $this->fixAmount = $amount;
        $this->userFee = $rate;
        $this->realname = $realname;
        $this->mobile = $mobile;
        $this->settleBankCard = $bankCardNo;
        $this->bankCardCode = $bankCardCode;
        $this->bankName = $bankCardName;
        $this->subMerchantNo = $subMerchantNo;
        $dataContent = $this->requestParams('042');
        return $this->getRequestResult($dataContent, $this->landingECollectMoneyQuickModifyFeeApi);
    }
}